package com.boarsoft.boar.soagov.svc.action;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.boarsoft.bean.ReplyInfo;
import com.boarsoft.boar.entity.ComInfo;
import com.boarsoft.boar.entity.SvcInfo;
import com.boarsoft.boar.soagov.com.ComBiz;
import com.boarsoft.boar.soagov.svc.SvcBiz;
import com.boarsoft.common.Util;
import com.boarsoft.common.action.PageAction;
import com.boarsoft.common.dao.PagedResult;
import com.boarsoft.common.util.JsonUtil;
import com.boarsoft.common.util.RandomUtil;
import com.boarsoft.flow.store.SimpleFlowStore;

@Component
@Scope("prototype")
@ParentPackage("default-package")
@Namespace(value = "/svc")
public class SvcAction extends PageAction {
	private static final long serialVersionUID = 6782184011310499284L;

	@Autowired
	private SvcBiz svcBiz;
	@Autowired
	private ComBiz comBiz;
	@Autowired
	private SimpleFlowStore flowStore;

	private String key;

	private String id;
	private String code;
	private String name;
	private String memo;

	@Action("get")
	public void get() {
		SvcInfo o = svcBiz.get(id);
		writeReply(true, JsonUtil.from(o, "face"));
	}

	@Action("save")
	public void save() {
		SvcInfo f = null;
		ComInfo c = null;
		boolean isNew = Util.strIsEmpty(id);
		if (isNew) {
			id = RandomUtil.randomUUID();
			f = new SvcInfo();
			f.setId(id);
			c = new ComInfo();
			c.setId(id);
		} else {
			f = svcBiz.get(id);
			c = comBiz.get(id);
		}
		f.setCode(code);
		f.setName(name);
		f.setType(SvcInfo.TYPE_SRVC);
		// f.setUrl(url);
		// f.setPath(path);// 导出路径
		f.setStatus(SvcInfo.STATUS_NOUSE);
		f.setMemo(memo);
		svcBiz.save(f);

		// 无论这个服务是否真的有流程，都保存一个对应的SUBFLOW组件
		// 除非这个SUBFLOW在其它流程中被显示引用，否则这个组件在树上看不到
		c.setId(f.getId());
		c.setCode(code);
		c.setName(name);
		c.setType(ComInfo.TYPE_SUBFLOW);
		c.setStatus(ComInfo.STATUS_NOUSE);
		// c.setJson(json);
		c.setMemo(memo);

		if (isNew) {
			comBiz.save(c);
		} else {
			comBiz.update(c);
		}

		writeReply();
	}

	@Action("list")
	public void list() {
		PagedResult<SvcInfo> pr = svcBiz.list(null, key, orderBy, pageNo, pageSize);
		writeReply(true, JsonUtil.from(pr, "face"));
	}

	@Action("delete")
	public void delete() {
		String[] aa = key.split(",");
		for (String id : aa) {
			ReplyInfo<String> ri = svcBiz.delete(id);
			if (ri.isSuccess()) {
				flowStore.delete(id);
			} else {
				writeReply(ri);
				return;
			}
		}
		writeReply();
	}

	public String getKey() {
		return key;
	}

	public void setKey(String key) {
		this.key = key;
	}

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getMemo() {
		return memo;
	}

	public void setMemo(String memo) {
		this.memo = memo;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}
}
